# -*- coding: utf-8 -*-
# @Time    : 2018/8/27 下午4:42
from rest_framework.pagination import LimitOffsetPagination as OriginLimitOffsetPagination
from rest_framework.response import Response
from collections import OrderedDict


class LimitOffsetPagination(OriginLimitOffsetPagination):
    max_limit = 20
    default_limit = 20
    offset_query_param = 'next'

    def get_next_link(self):
        if self.offset + self.limit >= min(self.count, 5000):
            return None

        return self.offset + self.limit

    def get_paginated_response(self, data):
        next = self.get_next_link()
        return Response(OrderedDict([
            ('next', str(next) if next is not None else next),
            ('list', data)
        ]))
